package com.sa.app.utilities;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class MyApplication extends Application {

	private DataBaseManager dataBaseManager;

	private LocationManager locationManager;
	private LocationListener locationListener;
	private Location currentBestLocation;

	public static final Float MIN_GPS_ACCURACY = Float.valueOf(180f);

	// insertUsed
	public static final Integer TAGGED = 0;
	public static final Integer VIRTUAL = 1;
	public static final Integer REAL = 2;
	public static final Integer MAIN = 3;

	// Ordination
	public static final Integer ALIAS = 0;
	public static final Integer MANUFACTURER = 1;
	public static final Integer MOST_USED = 2;

	@Override
	public void onCreate() {
		dataBaseManager = new DataBaseManagerImpl("W",
				this.getApplicationContext());

		initializeGPS();

		super.onCreate();
	}

	public DataBaseManager getDataBase() {
		return dataBaseManager;
	}

	public Location getLocation() {
		return currentBestLocation;
	}

	public Float getMinGpsAccuracy() {
		return MIN_GPS_ACCURACY;
	}

	public void initializeGPS() {
		// Configurar los paramentros de requestLocationUpdates para
		// que ahorre baterria y sean las actualziaciones y precision
		// suficiente y no se desperdicien recursos GPS
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationListener();
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);

	}

	// Objeto LocationListener propio
	private class MyLocationListener implements LocationListener {

		public void onLocationChanged(Location location) {

			Double lat = location.getLatitude();
			Double log = location.getLongitude();
			Log.i("GPS",
					lat + " : " + log + "ACCURANCY->" + location.getAccuracy());
			if (currentBestLocation != null
					&& currentBestLocation.getAccuracy() >= location
							.getAccuracy()) {

			} else if (currentBestLocation == null) {
				currentBestLocation = location;
			}

			if (location.getAccuracy() < MIN_GPS_ACCURACY) {
				locationManager.removeUpdates(locationListener);
			}

		}

		public void onProviderDisabled(String provider) {

		}

		public void onProviderEnabled(String provider) {

		}

		public void onStatusChanged(String provider, int status, Bundle extras) {

		}

	}

	public static Drawable resizeImage(BitmapDrawable b, int w, int h) {

		// load the origial Bitmap
		Bitmap BitmapOrg = b.getBitmap();

		int width = BitmapOrg.getWidth();
		int height = BitmapOrg.getHeight();
		int newWidth = w;
		int newHeight = h;

		// calculate the scale
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		// create a matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the Bitmap
		matrix.postScale(scaleWidth, scaleHeight);

		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
				height, matrix, true);

		// make a Drawable from Bitmap to allow to set the Bitmap
		// to the ImageView, ImageButton or what ever
		return new BitmapDrawable(resizedBitmap);

	}

}
